import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { priority } from "utils/constants/definetypes";
import { generateUniqueId } from "utils/utils";

 

export class  PowerCreepArgHelper
{

    public static getPowerCreepTask<T extends IPowerArg>(id:string,room:string,code:pc_behavior_key,arg:T,storeid:Id<AnyStoreStructure | Creep>,pri:number=priority.S,expire:number=1000):ITask< IPowerCreepData<"userPower">>
    {
        const taskarg:IPowerCreepData<"userPower">={
            room: room,
            OPSstoreId: storeid,
            arg: arg,
            state: ETaskState.Running
        };
        const task :ITaskDeclare["pc"]=
        {
            id:id?id:generateUniqueId(),
            ttl:1000,
            priority:pri,
            type:"pc",
            data:taskarg,
            shard:1,
        }
        return task;
    }


    public static getOpRoomObjectTask(e:RoomObject& _HasId,effect:PowerConstant,roomName:string,pri:number=priority.S)
    {
        const result =_.find(e.effects,e=>e.effect == effect);
        if(result)
        {
            return undefined;
        }
        const coreModule =  Game.rooms[roomName].getEntity("CoreEntity");
        const objs = coreModule.getTakeStores ({resourceType:RESOURCE_OPS,amount:100});
        let ops_store_id = objs[0];
        if(!ops_store_id)
        {
            ops_store_id = coreModule.Terminal;
        }

        const oplab:IPowerArg={POWER:effect,ObjectID:e.id};
        const optask = PowerCreepArgHelper.getPowerCreepTask(`${roomName}_op${effect}`,roomName,"userPower",oplab,ops_store_id.id,pri);
        return optask
    }

    
    public static getOpRoomObjectEvent<T extends RoomObject& _HasId>(e:T,effect:PowerConstant,roomName:string, leasttrm?:number)
    {
        const result =_.find(e.effects,e=>e.effect == effect);
        if(result && (!leasttrm  || result.ticksRemaining>leasttrm) )
        {
            return undefined;
        }
        
        // const coreModule =  Game.rooms[roomName].getEntity("CoreEntity");
        // const objs = coreModule.getTakeStores ({resourceType:RESOURCE_OPS,amount:100});
        // let ops_store_id = objs[0];
        // if(!ops_store_id)
        // {
        //     ops_store_id = coreModule.Terminal;
        // }
        // 可能需要指定仓库id
        const opevent:PowerEvent={
            POWER: effect, 
            ObjectID: e.id,
            region: roomName,
            room: e.pos.roomName
        };
        return opevent
    }
}
